import pyautogui
import operate as ope
import run_logging as log
import message as msg

work_image_path = '../../image/work\\'
mon_image_path = '../../image/monitor\\'
ope_image_path = '../../image/operate\\'
# 指定要查找的图片路径

target_inspect = 4  # 目标持续次数


def on_work():
    ope.random_sleep(10, 0)
    pyautogui.hotkey('alt', 'tab')
    log.log('开始作业')
    # 小行星带数量
    belt_count = 0
    # 释放无人机次数
    attack_count = 0
    while True:
        while ope.find_image(work_image_path + "xingdai.png") is not None:
            ope.random_sleep(3, 1)
            # 找到所有匹配的元素
            positions = list(pyautogui.locateAllOnScreen(work_image_path + 'xingdai.png'))
            log.log(f'找到{len(positions)}个星带')
            while positions is not None:
                # 检查是否找到了匹配的元素
                for position in positions:
                    if ope.find_image(work_image_path + "xingdai.png") is None:
                        break
                    belt_count += 1
                    # 获取元素的中心坐标
                    x, y = pyautogui.center(position)
                    # 右键点击该元素
                    ope.click_right_position(x, y)
                    if ope.find_image(work_image_path + "jump_to_0.png"):
                        ope.click_image(ope.find_image(work_image_path + "jump_to_0.png"))
                        log.log(f'进入第{belt_count}个星带')
                        ope.random_sleep(20, 0)
                        while ope.is_jumping():
                            ope.random_sleep(5, 0)
                        # 检查是否有怪
                        if (ope.find_image(work_image_path + "FFX.png") is not None
                                or ope.find_image(work_image_path + "DD.png") is not None
                                or ope.find_image(work_image_path + "BB.png") is not None
                                or ope.find_image(work_image_path + "BCP.png") is not None
                                or ope.find_image(work_image_path + "BC.png") is not None
                                or ope.find_image(work_image_path + "km.png") is not None):
                            ope.random_sleep(5, 0)
                            # 释放无人机
                            ope.release_UVA_attack()
                            attack_count += 1
                            log.log(f'第{attack_count}次释放无人机')
                            # st.log_to_window(f'第{attack_count}次释放无人机')
                            if ope.find_exact_image(work_image_path + "km.png") is not None:
                                attack_km()
                            # 记录状态持续次数
                            inspect = 0
                            while True:
                                # 检查无人机是否在战斗
                                if ope.find_image(work_image_path + 'standby.png'):
                                    inspect += 1  # 状态持续时间加一秒
                                    ope.random_sleep(3, 0)
                                    if inspect >= target_inspect:
                                        # 执行操作
                                        while ope.is_UAV_back() is False:
                                            ope.call_back_UVA()
                                            ope.random_sleep(3, 1)
                                        # 检查是否有boss
                                        if ope.find_image(work_image_path + 'boss1.png') is not None:
                                            # 发送消息
                                            log.log('势力boss已出现')
                                            # 切换总览
                                            ope.click_path_with_key(work_image_path + 'boss1.png', 'Q')
                                            ope.click_image_path(work_image_path + 'ab.png')
                                            ope.random_sleep(2, 1)
                                            ope.click_image_path(work_image_path + 'collect.png')
                                            ope.random_sleep(10, 2)
                                            msg.send_msg('势力boss已出现', '胜利丁')
                                            exit_outer_loop = False
                                            while ope.find_image(work_image_path + 'boss1.png') is not None:
                                                ope.random_sleep(1, 0.2)
                                                while ope.find_image(
                                                        work_image_path + 'collect_button.png') is not None:
                                                    ope.click_image_path(work_image_path + 'collect_button.png')
                                                    log.log('势力boss已出现')
                                                    ope.click_image_path(work_image_path + 'close_window.png')
                                                    exit_outer_loop = True
                                                    break
                                                if exit_outer_loop:
                                                    break
                                            ope.click_image_path(ope_image_path + 'station.png')
                                            ope.random_sleep(1, 0)
                                            ope.click_image_path(work_image_path + 'jump_to_station.png')
                                            ope.random_sleep(15, 0)
                                            while ope.is_jumping():
                                                ope.random_sleep(5, 0)
                                            ope.click_image_path(work_image_path + 'container.png')
                                            ope.click_image(ope.find_image(work_image_path + 'collect.png'))
                                            # ope.click_position(500, 120)
                                            ope.click_position(550, 300)
                                            ope.random_sleep(2, 1)
                                            pyautogui.keyDown('ctrl')
                                            pyautogui.keyDown('a')
                                            ope.random_sleep(0.5, 0)
                                            pyautogui.keyUp('a')
                                            pyautogui.keyUp('ctrl')
                                            pyautogui.dragTo(1250, 250, duration=1)
                                            ope.click_image_path(work_image_path + 'trans_button.png')
                                            ope.click_image_path(work_image_path + 'close_window.png')
                                            ope.random_sleep(1, 0)
                                            ope.click_image_path(work_image_path + 'container.png')
                                        break
                                else:
                                    # 如果在站里则结束循环
                                    if ope.find_image(ope_image_path + 'out_station.png') is not None:
                                        positions = None
                                        break
                                    # 状态未持续，重置计时
                                    inspect = 0
                                    # 检查是否有反跳怪优先击杀
                                    if ope.find_image(work_image_path + 'fantiao.png') is not None:
                                        ope.click_image(
                                            ope.click_path_with_key(work_image_path + 'fantiao.png', 'ctrl'))
                                        ope.random_sleep(5, 1)
                                        pyautogui.keyDown('f')
                                        pyautogui.keyUp('f')
                                ope.random_sleep(5, 3)  # 每5秒检查一次
                # break

                # if positions is None or ope.find_image(work_image_path + "out_station.png") is not None:
            log.log('ow中方法执行完毕')
            break
            ope.random_sleep(10, 2)
    # stop_event.set()  # 结束线程
    # break


def attack_km():
    if ope.find_image(work_image_path + "km.png") is not None:
        km_positions = list(
            pyautogui.locateAllOnScreen(work_image_path + 'km.png', confidence=0.9))
        print(km_positions)
        for km_position in km_positions:
            # 获取元素的中心坐标
            x, y = pyautogui.center(km_position)
            # 右键点击该元素
            ope.click_position_with_key((x, y), 'ctrl')
            ope.random_sleep(10, 1)
            pyautogui.keyDown('f')
            pyautogui.keyUp('f')
            while ope.find_image(work_image_path + "km_locked.png") is None and ope.find_image(
                    work_image_path + "km.png") is None:
                break
    # 保存地点
    ope.save_location(ope.find_image(work_image_path + 'hkmjch.png'))
    msg.send_msg('矿母已击杀', '胜利丁')
    ope.random_sleep(20, 2)


on_work()
